/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.omg.CORBA.NameValuePair;

/**
 *
 * @author sl_ru
 */
public class DBcontroller
{

    public static boolean iSLiveEditing = true;

    static Map<String, Statement> DBstatements = new HashMap<String, Statement>();
    static Map<String, String> DBnames = new HashMap<String, String>();

    public static void init()
    {
        DBnames.put("text", "TextData");
        DBnames.put("answers", "PlayerAnswers");
        DBnames.put("scripts", "DiaScripts");
        try
        {
            Class.forName("org.sqlite.JDBC");
            for (Map.Entry<String, String> entry : DBnames.entrySet())
            {
                Connection c = DriverManager.getConnection("jdbc:sqlite:"+entry.getValue());
                c.setAutoCommit(false);
                DBstatements.put(entry.getKey(), c.createStatement());
            }
        }
        catch (Exception e)
        {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }

        Connection c;

    }

    public static ResultSet getVal(String tableName, String ID)
    {
        return null;
    }

    public static void updateVal(String tableName, String ID, String updateString)
    {

    }

    public static Map<String, String> parseVars(String varString)
    {
        Map<String, String> m = new HashMap<String, String>();

        String[] ps = varString.split("\\n");

        for (int i = 0; i < ps.length; i++)
        {
            String v = ps[i];
            String[] nameVal = v.split("=");
            m.put(nameVal[0], nameVal[1]);
        }

        return m;
    }

    public static String parseBackVars(Map<String, String> vars)
    {
        String s = "";
        int i = 0;
        for (Map.Entry<String, String> entry : vars.entrySet())
        {
            if (i != 0)
            {
                s += "\\n";
            }
            s += entry.getKey() + entry.getValue();
            i++;

        }
        return s;
    }
}
